package com.he.hsdyc.config;


import com.he.hsdyc.common.handle.parent2.SubscribeHandler2;
import com.he.hsdyc.common.handle.parent2.UnsubscribeHandler2;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnClass(WxMpService.class)
public class WechatConfig {
//
//    @Autowired
//    @Qualifier("unsubscribeHandler_user")
//    private UnsubscribeHandler_user unsubscribeHandler_user;
//
//    @Autowired
//    @Qualifier("subscribeHandler_user")
//    private SubscribeHandler_user subscribeHandler_user;
//
//    @Autowired
//    @Qualifier("scanHandler_user")
//    private ScanHandler_user scanHandler_user;

//
//    @Autowired
//    @Qualifier("subscribeHandler_teacher")
//    private SubscribeHandler_teacher subscribeHandler_teacher;
//    @Autowired
//    @Qualifier("unsubscribeHandler_teacher")
//    private UnsubscribeHandler_teacher unsubscribeHandler_teacher;
//    @Autowired
//    @Qualifier("scanHandler_teacher")
//    private ScanHandler_teacher scanHandler_teacher;

//
//    @Autowired
//    @Qualifier("unsubscribeHandler_parent")
//    private UnsubscribeHandler unsubscribeHandler_parent;
//    @Autowired
//    @Qualifier("subscribeHandler_parent")
//    private SubscribeHandler subscribeHandler_parent;
//    @Autowired
//    @Qualifier("scanHandler_parent")
//    private ScanHandler scanHandler_parent;


    //YES家长
    @Autowired
    @Qualifier("unsubscribeHandler_parent2")
    private UnsubscribeHandler2 unsubscribeHandler_parent2;
    @Autowired
    @Qualifier("subscribeHandler_parent2")
    private SubscribeHandler2 subscribeHandler_parent2;
//    @Autowired
//    @Qualifier("scanHandler_parent2")
//    private ScanHandler2 scanHandler_parent2;



    @Autowired
    private ServerProperties wechatProperties;


//
//
//
//    @Bean(name = "config_user")
//    public WxMpConfigStorage configStorage_user() {
//        WxMpInMemoryConfigStorage configStorage=new WxMpInMemoryConfigStorage();
//        //WxMpInRedisConfigStorage configStorage = new WxMpInRedisConfigStorage();
//        //configStorage.setJedis(jedis);
//        configStorage.setAppId(this.wechatProperties.getUserAppId());
//        configStorage.setSecret(this.wechatProperties.getUserSecret());
//        configStorage.setToken(this.wechatProperties.getUserToken());
//        configStorage.setAesKey(this.wechatProperties.getUserAesKey());
//        return configStorage;
//    }
//
//    @Bean(name = "wxMpService_user")
//    public WxMpService wxMpService_user(@Qualifier("config_user")WxMpConfigStorage configStorage_user) {
//        WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceImpl();
//        wxMpService.setWxMpConfigStorage(configStorage_user);
//        return wxMpService;
//    }
//
//    @Bean(name = "router_user")
//    public WxMpMessageRouter router_user(@Qualifier("wxMpService_user")WxMpService wxMpService_user) {
//        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService_user);
//
////        // 记录所有事件的日志 （异步执行）
////        newRouter.rule().handler(this.logHandler).next();
////
////
////
////        // 关注事件
////        newRouter.rule().async(false).msgType(WxConsts.XML_MSG_EVENT)
////                .event(WxConsts.EVT_SUBSCRIBE).handler(subscribeHandler)
////                .end();
////
//        // 关注事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler_user)
//                .end();
//        //扫码事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SCAN)
//                .handler(scanHandler_user).end();
//
//
//        // 取消关注事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.UNSUBSCRIBE)
//                .handler(unsubscribeHandler_user).end();
////
////        // 默认
////        newRouter.rule().async(false).handler(msgHandler).end();
//
//        return newRouter;
//    }
//
//
//    /**--------------------------------教师端配置------------------------------------*/
//    @Bean(name = "config_teacher")
//    public WxMpConfigStorage config_teacher() {
//        WxMpInMemoryConfigStorage configStorage=new WxMpInMemoryConfigStorage();
////        WxMpInRedisConfigStorage configStorage = new WxMpInRedisConfigStorage();
////        configStorage.setJedis(jedis);
//        configStorage.setAppId(this.wechatProperties.getTeacherAppId());
//        configStorage.setSecret(this.wechatProperties.getTeacherSecret());
//        configStorage.setToken(this.wechatProperties.getTeacherToken());
//        configStorage.setAesKey(this.wechatProperties.getTeacherAesKey());
//        return configStorage;
//    }
//
//    @Bean(name = "wxMpService_teacher")
//    public WxMpService wxMpService_teacher(@Qualifier("config_teacher")WxMpConfigStorage config_teacher) {
//        WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceImpl();
//        wxMpService.setWxMpConfigStorage(config_teacher);
//        return wxMpService;
//    }
//
//    @Bean(name = "router_teacher")
//    public WxMpMessageRouter router_teacher(@Qualifier("wxMpService_teacher")WxMpService wxMpService_teacher) {
//        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService_teacher);
//
////        // 记录所有事件的日志 （异步执行）
////        newRouter.rule().handler(this.logHandler).next();
////
////
////
//
//        // 关注事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler_teacher)
//                .end();
//        // 取消关注事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.UNSUBSCRIBE)
//                .handler(unsubscribeHandler_teacher).end();
//        //扫码事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SCAN)
//                .handler(scanHandler_teacher).end();
////
////        // 默认
////        newRouter.rule().async(false).handler(msgHandler).end();
//
//        return newRouter;
//    }
//
//
//    /**--------------------------------家长端配置------------------------------------*/
//    @Bean(name = "config_parent")
//    public WxMpConfigStorage config_parent() {
//        WxMpInMemoryConfigStorage configStorage=new WxMpInMemoryConfigStorage();
//        //WxMpInRedisConfigStorage configStorage = new WxMpInRedisConfigStorage();
//        //configStorage.setJedis(jedis);
//        configStorage.setAppId(this.wechatProperties.getParentAppId());
//        configStorage.setSecret(this.wechatProperties.getParentSecret());
//        configStorage.setToken(this.wechatProperties.getParentToken());
//        configStorage.setAesKey(this.wechatProperties.getParentAesKey());
//        return configStorage;
//    }
//
//    @Bean(name = "wxMpService_parent")
//    public WxMpService wxMpService_parent(@Qualifier("config_parent")WxMpConfigStorage config_parent) {
//        WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceImpl();
//        wxMpService.setWxMpConfigStorage(config_parent);
//        return wxMpService;
//    }
//
//    @Bean(name = "router_parent")
//    public WxMpMessageRouter router_parent(@Qualifier("wxMpService_parent")WxMpService wxMpService_parent) {
//        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService_parent);
//
////        // 记录所有事件的日志 （异步执行）
////        newRouter.rule().handler(this.logHandler).next();
//
//
//
//        // 关注事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler_parent)
//                .end();
//
//        // 取消关注事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.UNSUBSCRIBE)
//                .handler(unsubscribeHandler_parent).end();
//
//        //扫码事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SCAN)
//                .handler(scanHandler_parent).end();
//
////        // 默认
////        newRouter.rule().async(false).handler(msgHandler).end();
//
//        return newRouter;
//    }
//


    /**--------------------------------YES家长端配置------------------------------------*/
    @Bean(name = "config_parent2")
    public WxMpConfigStorage config_parent2() {
        WxMpInMemoryConfigStorage configStorage=new WxMpInMemoryConfigStorage();
//        WxMpInRedisConfigStorage configStorage = new WxMpInRedisConfigStorage();
//        configStorage.setJedis(jedis);
        configStorage.setAppId(this.wechatProperties.getParentAppId2());
        configStorage.setSecret(this.wechatProperties.getParentSecret2());
        configStorage.setToken(this.wechatProperties.getParentToken2());
        configStorage.setAesKey(this.wechatProperties.getParentAesKey2());
        return configStorage;
    }

    @Bean(name = "wxMpService_parent2")
    public WxMpService wxMpService_parent2(@Qualifier("config_parent2")WxMpConfigStorage config_parent2) {
        WxMpService wxMpService = new me.chanjar.weixin.mp.api.impl.WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(config_parent2);
        return wxMpService;
    }

    @Bean(name = "router_parent2")
    public WxMpMessageRouter router_parent2(@Qualifier("wxMpService_parent2")WxMpService wxMpService_parent2) {
        final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService_parent2);

//        // 记录所有事件的日志 （异步执行）
//        newRouter.rule().handler(this.logHandler).next();



        // 关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler_parent2)
                .end();

        // 取消关注事件
        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
                .event(WxConsts.EventType.UNSUBSCRIBE)
                .handler(unsubscribeHandler_parent2).end();
//
//        //扫码事件
//        newRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
//                .event(WxConsts.EventType.SCAN)
//                .handler(scanHandler_parent2).end();

//        // 默认
//        newRouter.rule().async(false).handler(msgHandler).end();

        return newRouter;
    }


}
